﻿Imports System.IO

Namespace Settings

    Public Class SettingsStore

        Private Const DEF_CONFIG_NAME As String = "vPager.cfg"
        Private Const FIELD_DELIM As String = "="

        Private mConfigDir As String = AppDomain.CurrentDomain.BaseDirectory
        Private mConfigFile As String
        Private mSettings As Hashtable
        Private mErrorMsgs As String = ""

        Public ReadOnly Property ConfigFile As String
            Get
                Return mConfigFile
            End Get
        End Property

        Public Sub New(ByRef settings As Hashtable)
            mConfigFile = mConfigDir + DEF_CONFIG_NAME
            Debug.WriteLine("Config File: " & mConfigFile)
            mSettings = settings
            reload()
        End Sub

        Friend Sub reload()
            Dim strLine As String
            Dim srReader As StreamReader
            Dim fields() As String
            Dim key As String
            Dim value As String
            Dim setting As BaseSetting

            If File.Exists(mConfigFile) Then
                Try
                    srReader = File.OpenText(mConfigFile)
                    Do While srReader.Peek() >= 0
                        strLine = srReader.ReadLine()
                        strLine = Trim(strLine)
                        If Mid(strLine, 1, 1) <> "#" And strLine.Length > 0 Then
                            fields = Split(strLine, FIELD_DELIM, 2)
                            If fields.Length = 2 Then
                                key = UCase(Trim(fields(0)))
                                value = Trim(fields(1))
                                ' Convert string representation of key into actual SettingKey
                                Dim sk As SettingKey =
                                    CType([Enum].Parse(GetType(SettingKey), key), SettingKey)
                                setting = mSettings(sk)
                                If Not IsNothing(setting) Then
                                    Try
                                        setting.quietSet(value)
                                    Catch ex As Exception
                                        addError("Error Config Key " & key & "- Set Error: " & ex.ToString)
                                    End Try

                                Else
                                    addError("Unknown Config Key: " & key)
                                End If
                            Else
                                addError("Unparseable Line: " & strLine)
                            End If
                        End If
                    Loop

                Catch ex As Exception
                    ' Problem with reading file
                    addError("Cannot Read Config File: " & mConfigFile)
                    addError("Error returned: " & ex.ToString)
                Finally
                    If Not IsNothing(srReader) Then
                        srReader.Close()
                        srReader.Dispose()
                    End If
                End Try
            End If
        End Sub

        Friend Sub save()
            Dim key As String
            Dim value As String

            Using sw As StreamWriter = New StreamWriter(mConfigFile, False)
                For Each entry As DictionaryEntry In mSettings
                    key = CType(entry.Key, SettingKey).ToString
                    value = CType(entry.Value, BaseSetting).valueAsString()
                    sw.WriteLine(key & FIELD_DELIM & value)
                Next
            End Using

        End Sub

        Private Sub addError(ByVal errMsg As String)
            If mErrorMsgs = "" Then
                mErrorMsgs = errMsg
            Else
                mErrorMsgs &= vbNewLine & errMsg
            End If
        End Sub

        Friend Function hasErrors() As Boolean
            Return mErrorMsgs.Length > 0
        End Function

    End Class

End Namespace

